#include <TinkerKit.h>

#include <Usb.h>
#include <AndroidAccessory.h>

AndroidAccessory accessory("Ostfold University and College",
                     "Mora",
                     "arduino board for mora project",
                     "1.0",
                     "http://www.hiof.no",
                     "0000000012345678");
                     
TKButton btn_next(I0); //next button at I0 pin
int btn_next_current_state = LOW; //current state of next button
int btn_next_last_state = LOW; //last state of next button
long btn_next_last_time_pressed = 0; // the last time the left button was toggled

TKButton btn_previous(I1); //previous button at I1 pin
int btn_previous_current_state = LOW;//current state of previous button
int btn_previous_last_state = LOW; //last state of select button
long btn_previous_last_time_pressed = 0; //the last time the select button was toggled

TKButton btn_cancel(I8); //cancel button at I8 pin
int btn_cancel_current_state = LOW;//current state of cancel button
int btn_cancel_last_state = LOW; //last state of select button
long btn_cancel_last_time_pressed = 0; //the last time the select button was toggled

TKButton btn_select(I9); //select button at I9
int btn_select_current_state = LOW; //current state of select button
int btn_select_last_state = LOW; //last state of select button
long btn_select_last_time_pressed = 0; //the last time the select button was toggled

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long debounce = 50;   // the debounce time, increase if the output flickers

void setup() {
    Serial.begin(9600);
}

void send_command(String command){
        byte usb_command[2];
        usb_command[0] = 0x1;
	if(command == "next"){
		Serial.println("Next button is pressed");	
                usb_command[1] = 1;
	}else if(command == "previous"){
		Serial.println("Previous button is pressed");	
                usb_command[1] = 2;
	}else if(command == "cancel"){
		Serial.println("Cancel button is pressed");
                usb_command[1] = 3;
	}else if(command == "select"){
		Serial.println("Select button is pressed");
                usb_command[1] = 4;
	}
        if (accessory.isConnected()) {
              usbWrite(usb_command);
        }
}

void usbWrite(byte* msg){
       accessory.write(msg, 2);       
}


void loop()
{
	if (millis() - btn_next_last_time_pressed > debounce) {
		btn_next_last_state = btn_next_current_state;
		btn_next_current_state = btn_next.get();

		if(btn_next_current_state == LOW && btn_next_last_state == HIGH)
			send_command("next");
        
		btn_next_last_time_pressed = millis();    
	}
	
	if (millis() - btn_previous_last_time_pressed > debounce) {
		btn_previous_last_state = btn_previous_current_state;
		btn_previous_current_state = btn_previous.get();

		if(btn_previous_current_state == LOW && btn_previous_last_state == HIGH)
			send_command("previous");
        
		btn_previous_last_time_pressed = millis();    
	}
	
	if (millis() - btn_cancel_last_time_pressed > debounce) {
		btn_cancel_last_state = btn_cancel_current_state;
		btn_cancel_current_state = btn_cancel.get();

		if(btn_cancel_current_state == LOW && btn_cancel_last_state == HIGH)
			send_command("cancel");
        
		btn_cancel_last_time_pressed = millis();    
	}
	
	if (millis() - btn_select_last_time_pressed > debounce) {
		btn_select_last_state = btn_select_current_state;
		btn_select_current_state = btn_select.get();

		if(btn_select_current_state == LOW && btn_select_last_state == HIGH)
			send_command("select");
        
		btn_select_last_time_pressed = millis();    
	}
        if (!accessory.isConnected()) {
              Serial.println("Usb is not connected");
        }	
}
